//
//  TRBehaviorVerifyRequest.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/23.
//  Copyright © 2020 com.taylor. All rights reserved.
//


#import "TRBehaviorVerifyRequest.h"
#import "TRRequestTool.h"
#import "MJExtension/MJExtension/MJExtension.h"
#import "UIImage+tool.h"

//验证请求的服务器地址
static NSString* const TRBehaviorVerifyRequestURLString = @"https://mirror.anji-plus.com/api/";
//static NSString * const TRBehaviorVerifyRequestURLString = @"http://127.0.0.1:8080/";

@implementation TRBehaviorVerifyRequest


+ (void)captchaAccept:(TRBehaviorVerifyType)type FinishedBlock:(void (^)(BOOL, TRBehaviorVerifyModel * _Nonnull))finishedBlock
{
    NSString *urlString = @"captcha/get";
    NSString *behaviorVerifyType = @"blockPuzzle";
    switch (type) {
        case puzzle:
            behaviorVerifyType = @"blockPuzzle";
            break;
        case clickWord:
            behaviorVerifyType = @"clickWord";
            break;
        default:
            break;
    }
    
    NSDictionary *params = @{
            @"captchaType": behaviorVerifyType,
            @"distinguishSignatureVerificationMethod":@"ios"
    };
    
    NSString *absoluteURL = [NSString stringWithFormat:@"%@%@", TRBehaviorVerifyRequestURLString, urlString];
    [[TRRequestTool shareManager] sendRequestWithAPI:absoluteURL withVC:nil withParams:params withClass:nil responseBlock:^(id  _Nonnull response, BOOL isError, NSString * _Nonnull errorMessage, NSInteger errorCode) {
        TRBehaviorVerifyModel *model = NULL;
        
        if (isError) {
            UIImage *image = [UIImage imageNamed:@"origin1"];
            UIImage *slidingImage = [UIImage imageNamed:@"slidingBlock1"];
            NSString *originBase64 = [image convertToBase64];
            NSString *slidingBlockBase64 = [slidingImage convertToBase64];
            
//            @property (nonatomic, copy) NSString *originalImageBase64;
//            @property (nonatomic, copy) NSString *jigsawImageBase64;
//            @property (nonatomic, copy) NSString *token;
//            @property (nonatomic, copy) NSString *secretKey;
//            @property (nonatomic, copy) NSString *result;
//            @property (nonatomic, strong) NSArray *wordList;
            NSDictionary *dict = @{ @"originalImageBase64":originBase64,
                                    @"jigsawImageBase64":slidingBlockBase64,
                                    @"token":@"123123231123333122312312312312131231"
            };
            NSString *str = [dict mj_JSONString];
            model = [TRBehaviorVerifyModel mj_objectWithKeyValues:str];
            isError = NO;
            
        } else {
            model = [TRBehaviorVerifyModel mj_objectWithKeyValues:[response mj_JSONObject]];
        }
        
        if (finishedBlock) {
            finishedBlock(!isError, model);
        }
    }];
}


+ (void)captchaCheck:(TRBehaviorVerifyType)type PointJson:(NSString *)pointJson Token:(NSString *)token FinishedBlock:(void(^)(BOOL result, TRBehaviorVerifyModel *model))finishedBlock
{
    NSString *urlString = @"captcha/check";
    NSString *behaviorVerifyType = @"blockPuzzle";
    
    switch (type) {
        case puzzle:
            behaviorVerifyType = @"blockPuzzle";
            break;
            
        case clickWord:
            behaviorVerifyType = @"clickWord";
            break;
            
        default:
            break;
    }
    
    NSDictionary *params = @{
                 @"pointJson" : pointJson,
                 @"captchaType": behaviorVerifyType,
                 @"token" : token,
                 @"distinguishSignatureVerificationMethod": @"ios"
             };
    
    [[TRRequestTool shareManager] sendRequestWithAPI:urlString withVC:nil withParams:params withClass:nil responseBlock:^(id  _Nonnull response, BOOL isError, NSString * _Nonnull errorMessage, NSInteger errorCode) {
        TRBehaviorVerifyModel *behaviorVerifyModel;
        if (isError) {
            behaviorVerifyModel = NULL;
        } else {
            behaviorVerifyModel = [TRBehaviorVerifyModel mj_objectWithKeyValues:[response mj_JSONObject]];
        }
        
        if (finishedBlock) {
            finishedBlock(!isError, behaviorVerifyModel);
        }
    }];
}
@end
